<template>
  <div class="withdrawal-review-modal" v-if="visible">
    <div class="modal-overlay" @click="handleCancel"></div>
    <div class="modal-container">
      <!-- 标题栏 -->
      <div class="modal-header">
        <h3 class="modal-title">提现审核</h3>
        <button class="close-button" @click="handleCancel">
          <i class="fas fa-times"></i>
        </button>
      </div>

      <!-- 模态框内容 -->
      <div class="modal-body">
        <!-- 申请信息部分 -->
        <div class="application-info">
          <h4 class="section-title">申请信息</h4>

          <div class="info-row">
            <div class="info-label">申请ID</div>
            <div class="info-value">{{ withdrawal.id }}</div>
          </div>

          <div class="info-row">
            <div class="info-label">申请人</div>
            <div class="info-value">
              {{ withdrawal.username }}
              <span class="user-type" :class="withdrawal.userType === '区域代理' ? 'agent' : 'distributor'">
                {{ withdrawal.userType }}
              </span>
            </div>
          </div>

          <div class="info-row">
            <div class="info-label">申请时间</div>
            <div class="info-value">{{ withdrawal.applicationDate }}</div>
          </div>

          <div class="info-row">
            <div class="info-label">提现金额</div>
            <div class="info-value amount">¥{{ withdrawal.amount.toFixed(2) }}</div>
          </div>

          <div class="info-row">
            <div class="info-label">收款方式</div>
            <div class="info-value">{{ withdrawal.paymentMethod }}</div>
          </div>

          <div class="info-row">
            <div class="info-label">收款账号</div>
            <div class="info-value">{{ withdrawal.accountNumber }}</div>
          </div>
        </div>

        <!-- 分割线 -->
        <div class="divider"></div>

        <!-- 审核表单部分 -->
        <div class="review-form">
          <h4 class="section-title">审核结果</h4>

          <div class="radio-group">
            <div class="radio-option">
              <input type="radio" id="approve" value="approve" v-model="reviewResult" name="review-result">
              <label for="approve">通过</label>
            </div>
            <div class="radio-option">
              <input type="radio" id="reject" value="reject" v-model="reviewResult" name="review-result">
              <label for="reject">拒绝</label>
            </div>
          </div>

          <!-- 拒绝原因，仅在选择"拒绝"时显示 -->
          <div class="form-group" v-if="reviewResult === 'reject'">
            <label for="reject-reason">拒绝原因 <span class="required">*</span></label>
            <textarea
              id="reject-reason"
              v-model="rejectReason"
              rows="3"
              placeholder="请输入拒绝理由..."
              :class="{ 'input-error': showRejectReasonError }"
            ></textarea>
            <div class="error-message" v-if="showRejectReasonError">
              请输入拒绝原因
            </div>
          </div>

          <!-- 审核备注 -->
          <div class="form-group">
            <label for="review-remark">审核备注 (可选)</label>
            <textarea
              id="review-remark"
              v-model="reviewRemark"
              rows="2"
              placeholder="请输入审核备注..."
            ></textarea>
          </div>
        </div>
      </div>

      <!-- 底部按钮 -->
      <div class="modal-footer">
        <button class="cancel-button" @click="handleCancel">取消</button>
        <button
          class="submit-button"
          :disabled="isSubmitting"
          @click="handleSubmit"
        >
          <span v-if="isSubmitting">
            <i class="loading-spinner"></i>
            处理中...
          </span>
          <span v-else>确认</span>
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue';

// 定义props
interface WithdrawalApplication {
  id: string;
  username: string;
  userType: string;
  applicationDate: string;
  amount: number;
  paymentMethod: string;
  accountNumber: string;
  status: string;
  remark?: string;
  processDate?: string;
  transferDate?: string;
}

interface Props {
  visible: boolean;
  withdrawal: WithdrawalApplication;
}

// 定义事件
const emit = defineEmits<{
  (e: 'update:visible', visible: boolean): void;
  (e: 'approve', data: { id: string, remark: string }): void;
  (e: 'reject', data: { id: string, reason: string, remark: string }): void;
  (e: 'cancel'): void;
}>();

// 定义默认值
const defaultWithdrawal: WithdrawalApplication = {
  id: '',
  username: '',
  userType: '',
  applicationDate: '',
  amount: 0,
  paymentMethod: '',
  accountNumber: '',
  status: '待审核'
};

const props = withDefaults(defineProps<Props>(), {
  visible: false,
  withdrawal: () => ({
    id: '',
    username: '',
    userType: '',
    applicationDate: '',
    amount: 0,
    paymentMethod: '',
    accountNumber: '',
    status: '待审核'
  })
});

// 组件状态
const reviewResult = ref<'approve' | 'reject'>('approve');
const rejectReason = ref('');
const reviewRemark = ref('');
const isSubmitting = ref(false);
const showRejectReasonError = ref(false);

// 监听审核结果变化，重置错误状态
watch(reviewResult, () => {
  showRejectReasonError.value = false;
});

// 方法：验证表单
function validateForm(): boolean {
  let isValid = true;

  // 如果选择拒绝，但未填写拒绝原因
  if (reviewResult.value === 'reject' && !rejectReason.value.trim()) {
    showRejectReasonError.value = true;
    isValid = false;
  } else {
    showRejectReasonError.value = false;
  }

  return isValid;
}

// 方法：处理提交
async function handleSubmit() {
  // 表单验证
  if (!validateForm()) return;

  isSubmitting.value = true;

  try {
    // 模拟API请求延迟
    await new Promise(resolve => setTimeout(resolve, 800));

    // 根据审核结果触发不同事件
    if (reviewResult.value === 'approve') {
      emit('approve', {
        id: props.withdrawal.id,
        remark: reviewRemark.value
      });
    } else {
      emit('reject', {
        id: props.withdrawal.id,
        reason: rejectReason.value,
        remark: reviewRemark.value
      });
    }

    // 重置表单并关闭模态框
    resetForm();
    emit('update:visible', false);
  } catch (error) {
    console.error('审核提交失败:', error);
    // 实际项目中应显示错误提示
  } finally {
    isSubmitting.value = false;
  }
}

// 方法：处理取消
function handleCancel() {
  resetForm();
  emit('cancel');
  emit('update:visible', false);
}

// 方法：重置表单
function resetForm() {
  reviewResult.value = 'approve';
  rejectReason.value = '';
  reviewRemark.value = '';
  showRejectReasonError.value = false;
  isSubmitting.value = false;
}
</script>

<style scoped lang="scss">
$primary: #409eff;
$success: #67c23a;
$warning: #e6a23c;
$danger: #f56c6c;
$info: #909399;
$text-primary: #303133;
$text-regular: #606266;
$text-secondary: #909399;
$border-color: #ebeef5;
$border-light: #f2f6fc;
$background: #f5f7fa;

.withdrawal-review-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: -1;
}

.modal-container {
  background-color: white;
  border-radius: 4px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
  width: 90%;
  max-width: 550px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border-bottom: 1px solid $border-light;

  .modal-title {
    font-weight: 600;
    font-size: 18px;
    color: $text-primary;
    margin: 0;
  }

  .close-button {
    background: none;
    border: none;
    font-size: 18px;
    color: $text-secondary;
    cursor: pointer;

    &:hover {
      color: $text-primary;
    }
  }
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  color: $text-primary;
  margin: 0 0 16px 0;
}

.application-info {
  margin-bottom: 20px;
}

.info-row {
  display: flex;
  margin-bottom: 12px;

  .info-label {
    width: 100px;
    color: $text-secondary;
    font-size: 14px;
  }

  .info-value {
    flex: 1;
    color: $text-primary;
    font-size: 14px;

    &.amount {
      color: #ff7043;
      font-weight: 500;
    }
  }
}

.user-type {
  display: inline-block;
  padding: 0 6px;
  margin-left: 8px;
  line-height: 18px;
  font-size: 12px;
  border-radius: 10px;

  &.agent {
    background-color: #ecf5ff;
    color: $primary;
  }

  &.distributor {
    background-color: #f0f9eb;
    color: $success;
  }
}

.divider {
  height: 1px;
  background-color: $border-light;
  margin: 20px 0;
}

.review-form {
  .radio-group {
    display: flex;
    margin-bottom: 16px;
  }

  .radio-option {
    display: flex;
    align-items: center;
    margin-right: 24px;

    input[type="radio"] {
      margin-right: 8px;
    }

    label {
      font-size: 14px;
      color: $text-primary;
      cursor: pointer;
    }
  }
}

.form-group {
  margin-bottom: 16px;

  label {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
    color: $text-regular;

    .required {
      color: $danger;
      margin-left: 4px;
    }
  }

  textarea {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid $border-color;
    border-radius: 4px;
    font-size: 14px;
    font-family: inherit;
    resize: vertical;

    &:focus {
      outline: none;
      border-color: $primary;
    }

    &.input-error {
      border-color: $danger;
    }
  }

  .error-message {
    font-size: 12px;
    color: $danger;
    margin-top: 4px;
  }
}

.modal-footer {
  padding: 16px 20px;
  border-top: 1px solid $border-light;
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

.cancel-button, .submit-button {
  padding: 8px 20px;
  border-radius: 4px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s;
}

.cancel-button {
  background-color: white;
  border: 1px solid $border-color;
  color: $text-regular;

  &:hover {
    background-color: $background;
    color: $text-primary;
  }
}

.submit-button {
  background-color: $primary;
  border: 1px solid $primary;
  color: white;

  &:hover {
    background-color: darken($primary, 8%);
  }

  &:disabled {
    background-color: lighten($primary, 20%);
    border-color: lighten($primary, 20%);
    cursor: not-allowed;
  }
}

.loading-spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 50%;
  border-top-color: white;
  animation: spin 1s infinite linear;
  margin-right: 6px;
  vertical-align: middle;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 576px) {
  .info-row {
    flex-direction: column;

    .info-label {
      width: 100%;
      margin-bottom: 4px;
    }
  }

  .modal-container {
    max-height: 90vh;
  }
}
</style>
